﻿#ifndef FUNCTIONFORM_H
#define FUNCTIONFORM_H

#include <QWidget>
#include <QSettings>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include "WaveForm.h"
#include "common/DataType.h"
class QGroupBox;
class TimeEditForm;
class QDoubleSpinBox;

namespace Ui {
class FunctionForm;
}

class FunctionForm : public QWidget
{
    Q_OBJECT

public:
    explicit FunctionForm(QWidget *parent = nullptr);
    ~FunctionForm();

    //设置界面的下标   Ini配置类 和创建电流电压存CSV的File
    void setSettings(int index,QSettings* settings);

    void setBigPower();

    int getIndex();//获取下标

    void ReSetTings(QSettings *settings);           //重置的QSettings

    void setRunState(bool isRun);                   //设置运行状态

    void setConnectionState(bool isConnectionState);//设置连接状态

    void setFaultState(bool faultState);            //设置总故障状态、

    void setCurFaultState(bool faultState);         //设置电流故障状态、

    void setVolFaultState(bool faultState);         //设置电压故障状态、

    void setTempFaultState(bool faultState);         //设置温度故障状态、


    void setEleCurrentValue(float fEleCurrent);     //设置电流

    void setEleVolValue(float fEleVol);             //设置电压

    void setReMainValue(int iReMain1,int iReMain2); //设置剩余次数

    void setScreenSize(QSize size);                 //设置屏幕分辨率

    void setWriteCSV(bool isWrite);                 //写CSV

    void setDisconnectForRun(bool isDisconnect);    //设置断开的状态

    void setGroupBoxTitleText(bool isShow);

public slots:
    void  slot_SecondsChanged(int index,quint64 seconds);
    void  slot_EleCurrentChanged(double value);
    void  slot_btnParEnable(bool isClicked);

private slots:
    void on_cbox_TimeShow_currentIndexChanged(int index);

    void on_spinBox_Repetitions_valueChanged(int arg1);

    /*
    void on_spinBox_Remain_valueChanged(int arg1);
    */

    void on_btnCurValue_clicked();

    void on_dSpinBox_startElecurrent_valueChanged(double arg1);

    void on_btnSaveCSV_clicked();

    void on_btnLoadCSV_clicked();

    void on_btnRun_clicked();

    void on_btnMode_1_clicked();

    void on_btnMode_2_clicked();

    void on_btnMode_3_clicked();

    //void on_doubleSpinBoxCurValue_valueChanged(double arg1);

    void slot_SubFormClose();//子界面关闭的槽函数

    void timeoutWatchDog();//定时器槽函数

    void timeoutWatchDog_delay();//定时器槽函数

    /*
    void on_btnEleCurCSV_clicked(bool checked);//实时电流存CSV

    void on_btnEleVolCSV_clicked(bool checked);//实时电压存CSV
    */


    void on_btnMode_1_pressed();

    void on_btnMode_2_pressed();

    void on_btnMode_3_pressed();

    void on_btn_Link_clicked();

    //void on_doubleSpinBoxVolValue_valueChanged(double arg1);

    void on_btnVolValue_clicked();

    void on_btnReset_clicked();

    void on_btnReset_2_clicked();



    void on_btnUpdate_clicked();

protected :
    bool eventFilter(QObject *watched,QEvent *event) override;

signals:
    //void sig_SetWriteStartAddressAndRegisterCount(int startAddress, int registerCount,QList<quint16> data);

    void sig_SetTimeFormat(int format);
    void sig_SetRunState(bool isRun,int index,bool Disconnect = false);//修改运行状态, 第二个参数是16个子界面的下标
    void sig_Reset(int index);//复位，参数是当前界面的下标
    void sig_GeneralReset();//总复位

    void sig_ReSetIniFlie(QSettings* settings);


    void sig_AddWriteQueue(WriteData* data);//写消息队列使用

    void sig_showMessage(QString msg);//发送主界面故障信息

    void sig_openIPSet(bool isOpen);

private:
    //设置当前段的使能，当前 段到段后面的参数是 true则开，false则关
    void setCurrentParagraphEnable(int CurrentParagraph,bool Enable,bool isInit = false);
    void disableAllChildWidgets(QGroupBox *groupBox,bool isEnable);//设置运行时禁用


    void SendRunCommand(bool isRunning = false);//发送指令  参数是否运行中的指令

    QSettings* m_Settings;
    int m_Index;
    int m_CurrentParagraph;
    bool m_CurrentParagraphEnable;

    Ui::FunctionForm *ui;

    const QString BtnRunGray =
    "QPushButton"
    "{"
        "background-color: gray;"
        "border: none;"
        "border-radius: 3px;"
        "color: white;"
        "padding: 4px 12px;"
        "font-size: 10pt;"
    "}"
   "QPushButton:hover"
   "{"
        "background-color: rgb(150,150,150);"
   "}"
    "QPushButton::pressed"
    "{"
         "background-color: rgb(150,150,150);"
    "}"
    ;

    const QString BtnRunBlue =
    "QPushButton"
    "{"
        "background-color: #2196f3;"
        "border: none;"
        "border-radius: 3px;"
        "color: white;"
        "padding: 4px 12px;"
        "font-size: 10pt;"
    "}"
   "QPushButton:hover"
   "{"
        "background-color: #63b8fd;"
   "}"
    "QPushButton::pressed"
    "{"
         "background-color: #63b8fd;"
    "}"
    ;

    bool isRunState;//是否在運行的状态
    bool isConState;//是否是连接的状态
    bool isfaultState;//是否是总故障的状态


    const QPixmap   offlineMap;
    const QPixmap   onlineMap;
    const QPixmap   faultMap;

    WaveForm* m_WaveForm;

//    QList<TimeEditForm*> m_TimeEditFormList;
//    QList<QDoubleSpinBox*> m_DoubleSpinBoxList;
//    QList<QPushButton*>  m_PushButtonList;

    float m_eleCurrentRange;
    float m_eleVolRange;

    QTimer* m_timer_CSV;//写CSV的定时器
    QTimer* m_timer_CSV_delay;//写CSV的定时器
    quint8  m_CSV_delay;//延迟定时器计数

//    QDateTime m_DateTimeCurrentPre;//记录上一个电流传入的时间
//    QDateTime m_DateTimeVolPre;    //记录上一个电压传入的时间


     QFile* m_File;//文件存储
     QTextStream* m_Out;//写文件流

     int m_CurrentMode;//当前模式
     QMessageBox msgBox;//提示框


     QSize m_ScreenSize;//屏幕像素

};

#endif // FUNCTIONFORM_H
